#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int INF = 0x0fffffff ;

const int MAXN = 11111 ;

struct Edge
{
    int t,len,next;
} edge[MAXN<<1];

int head[MAXN],tot;

void add_edge(int s,int t,int len)
{
    edge[tot].t=t;
    edge[tot].len = len;
    edge[tot].next = head[s];
    head[s] = tot++;
}

struct Edge_id
{
    int a,b,len;
} edge_id[MAXN];

int node[MAXN<<2];

int father[MAXN],dep[MAXN],num[MAXN],son[MAXN],top[MAXN],w[MAXN];
int tot_w;

void dfs(int u,int fa,int level)
{
    dep[u] = level;
    father[u]=fa;
    num[u]=1;
    son[u]=-1;
    int maxx=0;
    for(int e = head[u];e!=-1;e=edge[e].next)
    {
        int v = edge[e].t;
        if(v!=fa)
        {
            dfs(v,u,level+1);
            num[u]+=num[v];
            if(num[v]>maxx)
            {
                maxx = num[v];
                son[u]=v;
            }
        }
    }
}

void build(int u,int tp)
{
    w[u] = ++tot_w;
    top[u]=tp;
    if(son[u]!=-1)
        build(son[u],tp);
    for(int e = head[u];e!=-1;e =edge[e].next)
    {
        int v = edge[e].t;
        if(v!=father[u]&&v!=son[u])
            build(v,v);
    }
}

void update(int l,int r,int rt,int pos,int c)
{
    if(l==r)
    {
        node[rt]=c;
        return ;
    }
    int m = l+r>>1;
    if(pos<=m) update(lson,pos,c);
    else update(rson,pos,c);
    node[rt] = max(node[rt<<1],node[rt<<1|1]);
}

int query(int l,int r,int rt,int a,int b)
{
    if(a<=l&&b>=r)
        return node[rt];
    int m = l+r>>1;
    int cnt1=-INF,cnt2=-INF;
    if(a<=m)
        cnt1 = query(lson,a,b);
    if(b>m)
        cnt2 = query(rson,a,b);
    return max(cnt1,cnt2);
}

int find(int a,int b)
{
    int fa = top[a];
    int fb = top[b];
    int tmp =0 ;
    while(fa!=fb)
    {
        if(dep[fa]>dep[fb])
        {
            swap(a,b);
            swap(fa,fb);
        }
        tmp = max(tmp,query(1,tot_w,1,w[fb],w[b]));
        b = father[fb];
        fb = top[b];
    }
    if(a==b) return tmp;
    if(dep[a]>dep[b])
    {
        swap(a,b);
    }
    return max(tmp,query(1,tot_w,1,w[son[a]],w[b]));
}

int n;

void init()
{
    int root =1;
    dfs(root,root,1);
    tot_w = -1;
    build(root,root);
    for(int i=1;i<n;i++)
    {
        if(dep[edge_id[i].a]>dep[edge_id[i].b])
            swap(edge_id[i].a,edge_id[i].b);
        update(1,tot_w,1,w[edge_id[i].b],edge_id[i].len);
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int a,b,c;
        tot=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,c);
            add_edge(b,a,c);
            edge_id[i].a=a;
            edge_id[i].b=b;
            edge_id[i].len=c;
        }
        init();
        char str[11];
        while(1)
        {
            scanf("%s",str);
            if(str[0]=='D')
                break;
            int a,b;
            scanf("%d%d",&a,&b);
            if(str[0]=='C')
            {
                int va = edge_id[a].a;
                int vb = edge_id[a].b;
                if(dep[va]>dep[vb])
                    swap(va,vb);
                update(1,tot_w,1,w[vb],b);
            }
            else printf("%d\n",find(a,b));
        }
    }
    return 0;
}
